// pages/salesxg/salesxg.js
const app = getApp()
const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    new: true,
    oldname:'',
    level:1
  },
  //获取姓名
  nameinput(e){
    this.data.name = e.detail.value
  },
  //获取电话
  levelinput(e){
    this.data.level = e.detail.value
  },

  // 添加或者修改staff数据
  addorupdate() {
    console.log('点确认了')
    var that = this
    // var myreg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    if (that.data.name == ''){
      wx.showModal({
        title: '温馨提醒',
        content: '请输入团队长姓名！',
        showCancel: false
      })
    }
    else if (that.data.mobile == '') {
      wx.showModal({
        title: '温馨提醒',
        content: '请输入团队长级别！',
        showCancel: false
      })
    }else {
      if (that.data.new){
        db.collection('TeamLeader').add({
          data: {
            name:that.data.name,
            level:that.data.level,
          }
        })
        .then(res => {
          console.log(res)
          wx.showModal({
            title: '恭喜',
            content: '团队长添加成功！',
            showCancel: false,
            complete() {
              // 跳转到team页面
              wx.redirectTo({
                url: '/pages/sales/sales'
              })
            }
          })
        })
      }else{
        //修改数据
        db.collection('TeamLeader').where({
          name:that.data.oldname
        }).get({
          success:function(res){
            if(res.data.length == 1 ){
              console.log(that.data.name)
              console.log(that.data.level)
              wx.cloud.callFunction({
                name: 'leader_update',
                data: {
                  staffid: res.data[0]._id,
                  name:that.data.name,
                  level: that.data.level,
                }
              }).then(res => {
                console.log('信息修改成功',res)
                wx.showModal({
                  title: '恭喜',
                  content: '信息修改成功！',
                  showCancel: false,
                  complete() {
                    // 跳转到team页面
                    wx.redirectTo({
                      url: '/pages/sales/sales'
                    })
                  }
                })
              }).catch(err => {
                console.log(err)
              })
            }
          },
        })
      }
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    console.log('options',options.name)

    if (options.name) {
      that.setData({
        new:false
      })
      db.collection('TeamLeader')
      .where({
        name:options.name
      })
      .get({
        success:function(res){
          that.data.oldname = res.data[0].name
          console.log(that.data.oldname)
          that.setData({
            name:res.data[0].name,
            level:res.data[0].level,
          })
        },
        fail:function(err){
          console.log('err',err)
        }
      })
    }else{
      console.log('这次是添加数据')
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})